import { defineStore } from 'pinia'
import { MassageControllerService, UserControllerService } from '../../generated'

export const useUserStore = defineStore('user', {
  state: () => ({
    id: 0,
    userAccount: '',
    userAvatar: '',
    userRole: '',
    noRead: 0,
    transationEmission: 0
  }),
  getters: {
    getId: (state) => state.id,
    getUserAccount: (state) => state.userAccount,
    getUserAvatar: (state) => state.userAvatar,
    getUserRole: (state) => state.userRole,
    getNoRead: (state) => state.noRead,
    getTransationEmission: (state) => state.transationEmission
  },
  actions: {
    async getLoginUser() {
      const res = await UserControllerService.getLoginUserUsingGet()
      this.userAccount = res.data.userAccount
      this.id = res.data.id
      this.userAvatar = res.data.userAvatar
      this.userRole = res.data.userRole
    },
    async getUserNoRead() {
      const res = await MassageControllerService.getUnReadNumUsingGet()

      this.noRead = res.data
    },
    setTransationEmission(num: Number) {
      this.transationEmission = num
    },
    clearUser() {
      this.$state = null
    }
  },
  persist: true
})
